package com.dwin.dingnuo.ui.system;


import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dwin.dingnuo.R;
import com.dwin.dingnuo.http.DwinConstants;
import com.dwin.dingnuo.http.MyRetrofit;
import com.dwin.dingnuo.http.beans.BackModel;
import com.dwin.dingnuo.http.request.ModifyPasswordBean;
import com.dwin.dingnuo.ui.base.BaseActivity;
import com.dwin.dingnuo.utils.ToastUtil;
import com.dwin.dingnuo.views.NetLoadingDialog;
import com.google.gson.Gson;

import okhttp3.RequestBody;
import retrofit2.Response;

/**
 * 修改密码
 */
public class EditPasswordActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "EditPasswordActivity";
    private EditText et_password_old;
    private EditText et_password01, et_password02;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_password);

        initViews();
    }

    @Override
    protected void initViews() {
        findView(R.id.btn_header_left).setOnClickListener(this);
        TextView tv_header_title = findView(R.id.tv_header_title);
        tv_header_title.setText(R.string.modify_password);

        et_password_old = findView(R.id.et_password_old);
        et_password01 = findView(R.id.et_password01);
        et_password02 = findView(R.id.et_password02);
        findView(R.id.btn_modify).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_header_left:
                onBackPressed();
                break;

            case R.id.btn_modify: // 修改密码
                String pwdOld = et_password_old.getText().toString().trim();
                String password1 = et_password01.getText().toString().trim();
                String password2 = et_password02.getText().toString().trim();

                if (TextUtils.isEmpty(pwdOld)) {
                    ToastUtil.toastShort(this, "请输入当前密码");
                    et_password_old.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(password1)) {
                    ToastUtil.toastShort(this, "请输入新密码");
                    et_password01.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(password2)) {
                    ToastUtil.toastShort(this, "请输入确认密码");
                    et_password02.requestFocus();
                    return;
                }
                if (!password1.equals(password2)) {
                    ToastUtil.toastShort(this, "两次输入密码不一致！");
                    et_password02.requestFocus();
                    return;
                }
                if (password1.equals(pwdOld)) {
                    ToastUtil.toastShort(this, "新密码不能与老密码相同");
                    et_password01.requestFocus();
                    return;
                }
                if (isNetworkConnected(this)) {
                    modifyPassword(pwdOld, password1);
                } else {
                    ToastUtil.toastShort(this, R.string.net_error);
                }
                break;
        }
    }

    // 修改账户密码。
    private void modifyPassword(String pwdOld, String pwd) {
        NetLoadingDialog.showprogress(mContext, true);
        ModifyPasswordBean info = new ModifyPasswordBean(pwdOld, pwd);
        Gson gson = new Gson();
        String obj = gson.toJson(info);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), obj);
        MyRetrofit.getInstance().clientPwdUpdate(language, mUser.auth, body, new retrofit2.Callback<BackModel>() {
            @Override
            public void onResponse(retrofit2.Call<BackModel> call, Response<BackModel> response) {
                if (DwinConstants.HTTP_OK == response.body().code) {
                    ToastUtil.toastLong(mContext, "密码修改成功");
                    finish();
                } else if (DwinConstants.HTTP_TOKEN_100002 == response.body().code) {
                    userLogout();
                } else {
                    ToastUtil.toastShort(mContext, response.body().message);
                }
                NetLoadingDialog.dismissprogress();
            }

            @Override
            public void onFailure(retrofit2.Call<BackModel> call, Throwable t) {
                Log.d(TAG, "onFailure: ");
                NetLoadingDialog.dismissprogress();
            }
        });
    }


}
